package org.dsc.amafinder.view;

import org.dsc.amafinder.ASearch;
import org.dsc.amafinder.R;
import org.dsc.amafinder.control.Amf;
import org.dsc.sdk.DSC;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AFavorites extends Activity {
	private static final String TAG = "afr." + AFavorites.class.getSimpleName();
	private static final String ASIN = "ASIN";

	private SharedPreferences mShPrefs;
	private ListView paisesListView;
	private Button btnPais;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(TAG, "onCreate::" + toString());
		setContentView(R.layout.afavorites);

		mShPrefs = getPreferences(MODE_PRIVATE);
		mShPrefs.getAll();
		String pais = mShPrefs.getString(ASIN, "");
		Toast.makeText(getApplicationContext(), pais, Toast.LENGTH_SHORT)
				.show();

		String[] lista = getResources().getStringArray(R.array.paises);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lista);
		paisesListView = (ListView) findViewById(R.id.Paises);
		paisesListView.setAdapter(adapter);
		paisesListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String pais = ((TextView) view).getText().toString();
				Toast.makeText(getApplicationContext(), pais,
						Toast.LENGTH_SHORT).show();
//				Amf.ASIN = "es";
			}
		});

		btnPais = (Button) findViewById(R.id.btnPais);
		btnPais.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				saveAndExit();
			}
		});

      // poner adView
      DSC.A(this).createAdv(R.id.PubliLayoutOptions);
	}

   @Override
   protected void onResume() {
      super.onResume();
      DSC.A(this).resume();
   }

   @Override
   protected void onRestart() {
      super.onRestart();
      DSC.A(this).restart();
      finish();
   }

   @Override
   public void onPause() {
      DSC.A(this).pause();
      super.onPause();
   }

   @Override
   public void onDestroy() {
      DSC.A(this).destroy();
      super.onDestroy();
   }

	protected void saveAndExit() {
		SharedPreferences.Editor editor = mShPrefs.edit();
		editor.putString(ASIN, Amf.ASIN);
		editor.commit();
		finish();
	}

	/*
	 * @ini-menu-action-bar
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu items for use in the action bar
		getMenuInflater().inflate(R.menu.main_activity_actions, menu);
      menu.findItem(R.id.action_search).setIntent(new Intent(this, ASearch.class));
      menu.findItem(R.id.action_info).setIntent(new Intent(this, AInfo.class));
      menu.findItem(R.id.action_favorites).setIntent(new Intent(this, AFavorites.class));
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_search:
		case R.id.action_info:
			saveAndExit();
			startActivity(item.getIntent());
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}
	/*
	 * @end-menu-action-bar
	 */
}
